package com.ad.pet.app.shopcar.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ad.pet.R;
import com.ad.pet.models.remote.cart.Good;
import com.ad.pet.models.remote.cart.Shop;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by SEELE on 2018/4/18.
 */

public class ShopAdapter extends BaseQuickAdapter<Shop, BaseViewHolder> {
    private OnCheckChoseLinstener mOnCheckChoseLinstener;


    public ShopAdapter(@Nullable List<Shop> data) {
        super(R.layout.item_shop_layout, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Shop item) {

        final RecyclerView recyclerView = helper.getView(R.id.recycler_good);
        recyclerView.setFocusableInTouchMode(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
//        linearLayoutManager.setScrollEnabled(false);
        final GoodAdapter goodAdapter = new GoodAdapter(item.getGoods());

        recyclerView.setLayoutManager(linearLayoutManager);


        TextView tvSopname = helper.getView(R.id.tv_title);
        tvSopname.setText(item.getShopName());


        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(goodAdapter);


//        final ImageView imageView = helper.getView(R.id.iv_chose_shop);//商店选择图标
//        imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                int count = 0;
//                for (int i = 0; i < mData.get(helper.getLayoutPosition()).getGoods().size(); i++) {
//                    Good good = mData.get(helper.getLayoutPosition()).getGoods().get(i);
//                    if (good.getFree_shipping_99()==1){
//                        count++;
//                    }
//                    if (count>1){
//                        break;
//                    }
//                }
//                if (count>1){
//                    ToastUtils.showLongToast(mContext.getString(R.string.count99_tips));
//                    return;
//                }
//                mData.get(helper.getLayoutPosition()).setChose(!mData.get(helper.getLayoutPosition()).isChose());
//                for (int i = 0; i < mData.get(helper.getLayoutPosition()).getGoods().size(); i++) {
//                    mData.get(helper.getLayoutPosition()).getGoods().get(i).setChose(mData.get(helper.getLayoutPosition()).isChose());
//                }
//                mOnCheckChoseLinstener.onCheckChose(helper.getLayoutPosition(),-1);
//            }
//        });
//        imageView.setImageResource(item.isChose() ? R.drawable.ic_chose : R.drawable.ic_unchose);

        goodAdapter.setOnSelectClickLinstener(new GoodAdapter.OnSelectClickLinstener() {
            @Override
            public void onSelectClickLinstener(int position) {
                mOnCheckChoseLinstener.onCheckChose(helper.getLayoutPosition(),position);
                mData.get(helper.getLayoutPosition()).setChose(isShowAllChose(helper.getLayoutPosition()));
//                imageView.setImageResource(item.isChose() ? R.drawable.ic_chose : R.drawable.ic_unchose);


            }

            @Override
            public void onRemoveNumber(int position,int number) {
                mOnCheckChoseLinstener.onRemoveNumber(helper.getLayoutPosition(),position,number);
            }

            @Override
            public void onAddNumber(int position,int number) {
                mOnCheckChoseLinstener.onAddNumber(helper.getLayoutPosition(),position,number);
            }

            @Override
            public void onSetNumber(int position, int number) {
                mOnCheckChoseLinstener.onSetNumber(helper.getLayoutPosition(),position,number);
            }
        });

        goodAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
    }

    /**
     * 获取总价格
     *
     * @return
     */
    public double getAllPrice() {
        double allPrice = 0;
//        for (int i = 0; i < mData.size(); i++) {
//            for (Good good :
//                    mData.get(i).getGoods()) {
//                if (good.isChose()) {
//                    double itemGoodPrice = good.getPrice()*(Double.valueOf(good.getNumber()));
////                    allPrice =allPrice+itemGoodPrice;
//                    allPrice = LyxUtils.formatAdd(allPrice,itemGoodPrice);
//                }
//            }
//        }
        double f = allPrice;
//        BigDecimal b = new BigDecimal(f );
//        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f;
    }

    public interface OnCheckChoseLinstener {
        void onCheckChose(int layoutPosition,int positon);
        void onAddNumber(int shopPosition,int position,int number);
        void onRemoveNumber(int shopPosition,int position,int number);
        void onSetNumber(int shopPosition,int positon,int number);
    }

    public void setOnCheckChoseLinstener(OnCheckChoseLinstener onCheckChoseLinstener) {
        mOnCheckChoseLinstener = onCheckChoseLinstener;
    }

    /**
     * 店铺商品是否全选
     *
     * @param pos
     */
    private boolean isShowAllChose(int pos) {
        boolean isAllChose = true;
        for (Good good : mData.get(pos).getGoods()) {
            if (!good.isChose()) {
                isAllChose = false;
                break;
            }
        }
        return isAllChose;
    }

    /**
     * 店铺是否全选
     *
     * @param
     */
    public boolean isShopShowAllChose() {
        boolean isAllChose = true;
        for (Shop good : mData) {
            if (!good.isChose()) {
                isAllChose = false;
                break;
            }
        }
        return isAllChose;
    }

    /**
     * 给所有item复制
     */
    public void setAllChose(boolean flag) {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChose(flag);
            for (Good good : mData.get(i).getGoods()) {
                good.setChose(flag);
            }
        }
        notifyDataSetChanged();
    }
    /**
     * 管理删除选中的元素
     */
    public void removeAllChoseGood(Set<Good> goods){
        Iterator<Shop> iter = mData.iterator();
        while(iter.hasNext()){
            Shop s = iter.next();
            Iterator<Good> goodIterator = s.getGoods().iterator();
            while (goodIterator.hasNext()){
                Good good = goodIterator.next();
                if (good.isChose()){
                    goods.remove(good);
                    goodIterator.remove();
                }
            }
            if (s.getGoods().size()==0){
                iter.remove();
            }
        }
        notifyDataSetChanged();
    }
    /**
     * 管理删除选中的元素
     */
    public void removeAllChoseGood(){
        Iterator<Shop> iter = mData.iterator();
        while(iter.hasNext()){
            Shop s = iter.next();
            Iterator<Good> goodIterator = s.getGoods().iterator();
            while (goodIterator.hasNext()){
                Good good = goodIterator.next();
                if (good.isChose()){
                    goodIterator.remove();
                }
            }
            if (s.getGoods().size()==0){
                iter.remove();
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 获取所有选中的item
     */
    public List<Good> getAllChoseGoods(){
        List<Good> goods = new ArrayList<>();
        for (int i = 0; i < mData.size(); i++) {
            for (int j = 0; j < mData.get(i).getGoods().size(); j++) {
                if (mData.get(i).getGoods().get(j).isChose()){
                    goods.add(mData.get(i).getGoods().get(j));
                }
            }
        }
    return goods;
    }


    /**
     * 是否有选商品
     */
    public  boolean isHasChoseGood(){
        boolean isAllChose = false;
        for (int i = 0; i < mData.size(); i++) {
            for (int j = 0; j < mData.get(i).getGoods().size(); j++) {
                if (mData.get(i).getGoods().get(j).isChose()){
                    isAllChose=true;
                    break;
                }
            }
        }
        return isAllChose;
    }

    public void setShopAllChose(boolean ischose) {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChose(ischose);
        }
    }
    public boolean getShopIsAllChose(int position){
        boolean flag = true;
        for (int i = 0; i < mData.get(position).getGoods().size(); i++) {
                    if (!mData.get(position).getGoods().get(i).isChose()){
                        flag = false;
                        break;
                    }
        }
        return flag;
    }
}
